package net.zhengxinyun.performance.Util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import net.zhengxinyun.performance.entity.WarnEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.SixIndicatorsFilter;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import java.util.Date;

/**
 * @Auther: hyp
 * @Date: 2018/10/30 14:21
 */
public class TokenUtil {

    public static Long OVERTIME = 2592000000L;//一个月时间。设定该token的过期时间

    /**
     * 加密
     * @param id
     * @param issuer
     * @param subject
     * @param ttlMillis
     * @return
     */
    public static String createJWT(String id, String issuer, String subject, long ttlMillis) {
        //The JWT signature algorithm we will be using to sign the token
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);

        //We will sign our JWT with our ApiKey secret
        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary("4234dfasdfqdd");
        Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
		//Let's set the JWT Claims
        JwtBuilder builder = Jwts.builder().setId(id).setIssuedAt(now).setSubject(subject).setIssuer(issuer).signWith(signatureAlgorithm, signingKey);
        //if it has been specified, let's add the expiration
        if (ttlMillis >= 0) {
		long expMillis = nowMillis + ttlMillis;
		Date exp = new Date(expMillis);
		builder.setExpiration(exp);
        }
        //Builds the JWT and serializes it to a compact, URL-safe string
        return builder.compact();
    }

    /**
     * 解密权限
     * @param jwt
     */
    public static void parseJWT(String jwt) {
        //This line will throw an exception if it is not a signed JWS (as expected)
        Claims claims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary("4234dfasdfqdd")).parseClaimsJws(jwt).getBody();
        System.out.println("ID: " + claims.getId());
        System.out.println("Subject: " + claims.getSubject());
        System.out.println("Issuer: " + claims.getIssuer());
        System.out.println("Expiration: " + claims.getExpiration());
    }

    public static void test(Object object){
        System.out.println(object.getClass().getName());
    }


    public static void main(String[] args) {

       String s =  Util.doubleFormat(2.60);
        System.out.println(s);

//        TokenUtil t = new TokenUtil();
//        //加密
//        String jwt = t.createJWT("3133", "13dsdda", "afrefsa", 20171252);
//        System.out.println(jwt);
//
//        //解密
//        t.parseJWT(jwt);
    }
}
